package com.lym.controller;

import com.car.entity.CellPack;
import com.car.entity.SysUser;
import com.car.entity.TWhite;
import com.car.mapper.WhiteMapper;
import com.car.util.JwtUtil;
import com.car.util.MD5Util;
import com.car.util.Result;
import com.car.vo.CarInfoVo;
import com.github.pagehelper.PageInfo;
import com.lym.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;


/**
 * @ClassName BaseContoller
 * @Description: TODO
 * @Author ForeverLoveT
 * @Date 2020/3/9
 * @Version V1.0
 **/
@Controller
public class BaseController {
    @Autowired
    private BaseService baseService;
    @Autowired
    private JwtUtil jwtUtil;
    @GetMapping("findAll")
    public String login(Model model,@RequestParam(defaultValue = "")String name, @RequestParam(defaultValue = "")String vin, @RequestParam(defaultValue = "5")Integer pageSize, @RequestParam(defaultValue = "1")Integer pageNum){
        PageInfo pageInfo = baseService.findAll("%" + name + "%", vin, pageNum, pageSize);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("name",name);
        model.addAttribute("vin",vin);
        //获得当前页
        model.addAttribute("pageNum", pageInfo.getPageNum());
        //获得一页显示的条数
        model.addAttribute("pageSize", pageInfo.getPageSize());
        //是否是第一页
        model.addAttribute("isFirstPage", pageInfo.isIsFirstPage());
        //获得总页数
        model.addAttribute("totalPages", pageInfo.getPages());
        //是否是最后一页
        model.addAttribute("isLastPage", pageInfo.isIsLastPage());
        //获取总条数
        model.addAttribute("total",pageInfo.getTotal());
        List<CellPack> cell = baseService.getClell("0");
        model.addAttribute("cell",cell);
        return "list";
    }
    //回显电池
    /** 
     * @MethodName: getClell
     * @Description: TODO
     * @Return: java.util.List<com.car.entity.CellPack>
     * @Author: ForeverLoveT
     * @Date: 11:03
    **/
    //为配电
    @GetMapping("getcell")
    @ResponseBody
    public List<CellPack> getClell(HttpServletRequest request){
        List<CellPack> cell = baseService.getClell("0");
        return cell;
    }
    //peiidani
    /** 
     * @MethodName: PeiDian 获取当前的车id和电池的id 进行配电
     * @Description: TODO
     * @Param: [carid, cellid]
     * @Return: java.lang.String
     * @Author: ForeverLoveT
     * @Date: 12:08
    **/
    @ResponseBody
    @GetMapping("peidian")
    public String PeiDian(String carid,String cellid){
        baseService.PeiDian(carid,cellid);
        return "配电成功";
    }
    /** 
     * @MethodName: getCellOne 获取已配电的信息
     * @Description: TODO
     * @Param: [carid]
     * @Return: com.car.entity.CellPack
     * @Author: ForeverLoveT
     * @Date: 12:25
    **/
    @ResponseBody
    @GetMapping("findOneCell")
    public CellPack getCellOne(String carid){
        CellPack oneCell = baseService.findOneCell(carid);
        return oneCell;
    }
    /** 
     * @MethodName: getCarAll
     * @Description: TODO
     * @Param: [carid]
     * @Return: com.car.vo.CarInfoVo
     * @Author: ForeverLoveT
     * @Date: 18:26
    **/
    //查询车的详情
    @ResponseBody
    @GetMapping("getCarAll")
    public CarInfoVo getCarAll(String carid){
        CarInfoVo carAll = baseService.getCarAll(carid);
        System.err.println(carAll);
        return carAll;
    }
    //汽车添加
    @PostMapping("addcar")
    @ResponseBody
    public Result addCar(@RequestBody CarInfoVo carInfo){
        baseService.AddCar(carInfo);
        return new Result(true,"ok");
    }
    //登录发送token
    /**
     * 用户登录
     */
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private WhiteMapper whiteMapper;
    @RequestMapping(value = "Login",method = RequestMethod.POST)
    @ResponseBody
    public String  login(@RequestBody SysUser sysUser,HttpServletRequest request){
        SysUser user= baseService.getLogin(sysUser);
        if(user!=null){
            String token = MD5Util.MD5encode(user.getUsername()+user.getPassword()+user.getId());
            TWhite whiteById = whiteMapper.findWhiteById(user.getId());
            redisTemplate.delete("whiteIp");
            redisTemplate.opsForValue().set("whiteIp",whiteById.getIp());
        return token;
        }
        return "mei有";
    }
    @RequestMapping(value = "getApiKay",method = RequestMethod.GET)
    //http://car-base/addcar
    @ResponseBody
    public Result getApiKey(String url){

        String appKey = MD5Util.MD5encode(new Date().getTime() + "");

        redisTemplate.opsForValue().set(appKey,url); //http://car-base/addcar

        Result result = new Result(true,"AppKey生成成功!请做好记录！",appKey+"      <=AppKey====Value=>      "+url);

        return result;
    }

}